﻿#include "FITKCalculiXINPReader.h"
#include "FITKCCXAdaptorCase.h"
#include "FITKCCXAdaptorModel.h"
#include "InpKeyWordsChecker.h"
#include "FITK_Kernel/FITKAdaptor/FITKIOAdaptorFactory.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"

namespace IO
{

    FITKCalculiXINPReader::FITKCalculiXINPReader()
    {
        _keyWordsChecker = new InpKeyWordsChecker(this);
    }

    FITKCalculiXINPReader::~FITKCalculiXINPReader()
    {
        if (_keyWordsChecker) delete _keyWordsChecker;
    }

    void FITKCalculiXINPReader::run()
    {
        //加载文件
        bool ok = this->loadFile();
        if (!ok) return;
        //强制类型转换
        AbaqusData::FITKDataCase *caseData =
            AbaqusData::FITKDataCase::SafeDownCastFITKDataCase(_data);
        if (caseData == nullptr) return;

        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKCCXAdaptorCase>("CCXINP", caseData);
        if (adaptor == nullptr) return;
        adaptor->setDataObject(caseData);
        adaptor->setFileReader(this);
        ok = adaptor->adaptR();
        if (_resultMark)
            (*_resultMark) = ok;
        delete adaptor;
        //读取完成
        this->sendCurrentPercent(100);
    }

    QString FITKCalculiXINPReader::readLine()
    {
        QString line;
        while (!atEnd())
        {
            line = FITKAbstractTextReader::readLine().simplified();
            // 空行跳过
            if (line.isEmpty())continue;
            // 注释行跳过
            if (line.startsWith("**")) continue;
            break;
        }
        return line;
    }

    void FITKCalculiXINPReader::consoleMessage(int level, const QString& str)
    {
        //判断消息等级
        switch (level)
        {
        case 1:AppFrame::FITKMessageNormal(str); break;
        case 2: AppFrame::FITKMessageWarning(str); break;
        case 3:AppFrame::FITKMessageError(str); break;
        default: AppFrame::FITKMessageError(str); break;
        }
    }

    bool FITKCalculiXINPReader::checkKeyWords(const QString& header, const QStringList& keys, const QHash<QString, QString>& keyValues)
    {
        if (_keyWordsChecker)
            return _keyWordsChecker->check(header, keys, keyValues);
        return false;
    }
}


